package jnpf.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.base.ActionResult;
import jnpf.common.enums.dify.DifyAITypeEnum;
import jnpf.model.external.DifyAiCreateReq;
import jnpf.model.external.LoginReq;
import jnpf.model.external.RagAiCreateReq;
import jnpf.model.ragFlow.talkAssistant.ChatCreateReq;
import jnpf.service.DifyKnowService;
import jnpf.service.ExternalLoginService;
import jnpf.service.RagTalkAssistantService;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Tag(name = "external", description = "external")
@RequestMapping("/api/example/external")
public class ExternalController {
    @Autowired
    ExternalLoginService externalLoginService;
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private DifyKnowService difyKnowService;
    @Autowired
    private RagTalkAssistantService ragTalkAssistantService;


    @PostMapping("/login/{platform}")
    public ActionResult login(@RequestBody LoginReq request) throws Exception {
        return externalLoginService.apiLogin(request);
    }


    @PostMapping("/dify/createai")
    public ActionResult createApps(@RequestParam("username") String username,@RequestParam("password") String password, @RequestBody DifyAiCreateReq difyAiCreateReq) throws Exception {
        LoginReq loginReq = new LoginReq();
        loginReq.setEmail(username);
        loginReq.setPassword(password);
        return externalLoginService.createDifyAi(loginReq, difyAiCreateReq);
    }

//    @PostMapping("/dify/rag/ai")
//    public ActionResult createRagAi( @RequestBody RagAiCreateReq difyAiCreateReq) throws Exception {
//        ChatCreateReq chatCreateReq=new ChatCreateReq();
//        chatCreateReq.setName(difyAiCreateReq.getName());
//        return  difyKnowService.createDifyAi(difyAiCreateReq);
//    }

    @PostMapping("/dify/create/ai/{type}/{difyAi}")
    public ActionResult createDifyAI( @RequestBody DifyAiCreateReq difyAiCreateReq,@PathVariable("type") String type,@PathVariable("difyAi") String difyAi) throws Exception {
        LoginReq loginReq = new LoginReq();
        List<String> codeList = DifyAITypeEnum.getCodeList();
        if(!codeList.contains(difyAiCreateReq.getMode())){
            return ActionResult.fail("请选择正确的模式");
        }
        if(type.equals("1")){
            return difyKnowService.createDifyAi(difyAiCreateReq);
        }else{
            return ActionResult.success();
//            return difyKnowService.updateDifyAi(difyAiCreateReq,difyAi);
        }
    }



    @GetMapping("/token/{platform}")
    public ActionResult<String> getTokenByPlatform(@PathVariable String platform) {
        try {
            String token = externalLoginService.getTokenByPlatform(platform);
            if (StringUtil.isEmpty(token)) {
                return ActionResult.fail("未找到该平台的登录token");
            }
            return ActionResult.success(token);
        } catch (Exception e) {
            log.error("获取平台token失败", e);
            return ActionResult.fail("获取token失败: " + e.getMessage());
        }
    }

}
